﻿local nIndexThread
function longinClient(dm, index, threadIndex)
  show("开始1")
  while (GetState()) do
    --repeat
    SetCheckState(false)
    local n = GetBindWindow(dm)
    if (n > 0) then
      show("解除绑定" .. tostring(index))
      UnBindWindow(dm)
    --如果存在绑定则解除绑定
    end
    MoveTo(dm, 2, 5)
    show("打开游戏")
    hwnd = FindWindowEx(dm, "", "MapleStory")
    show("游戏开始窗口句柄存在关闭游戏" .. tostring(hwnd))
    if (hwnd ~= 0) then
      show("关闭游戏进程")
      pids = EnumProcess(dm, "GameLauncher.exe")
      show(pids)
      local tab = split(pids, ",")
      for key, value in ipairs(tab) do
        TerminateProcess(dm, value)
        delay(dm, 500)
      end
      pids = EnumProcess(dm, "MapleStory.exe")

      tab = split(pids, ",")
      for key, value in ipairs(tab) do
        TerminateProcess(dm, value)
        delay(dm, 500)
      end
    end
    runScript(dm, "游戏图标")
    delay(dm, 500)
    hwnd = FindWindowEx(dm, "", "MapleStory")
    if (hwnd ~= 0) then
      show("游戏引导已经打开")
      --SetWindowState(dm,hwnd,12)
      delay(dm, 1000)
      KeyPress(dm, "tab")
      delay(dm, 1000)
      KeyPress(dm, "enter")
      delay(dm, 10000)
    end

    for i = 1, 150 do
      pids = EnumProcess(dm, "GameLauncher.exe")
      if (pids ~= "") then
        show("窗口存在退出")
        break
      end

      pids = EnumProcess(dm, "MapleStory.exe")
      if (pids == "" or pids == nil) then
        show("游戏进程不存在重新开始")
        break
      end

      hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
      --show("查找窗口句柄" .. tostring(hwnd).."索引："..tostring(index).."账号："..GetItemText(index,1))
      show("等待启动.窗口句柄" .. tostring(hwnd))
      delay(dm, 1000)
      if (hwnd ~= 0) then
        ret = BindWindowEx(dm, hwnd)
        show("窗口绑定" .. tostring(ret))
        if (ret == 1) then
          delay(dm, 1000)
          MoveWindow(dm, hwnd, 0, 0)
          show("窗口绑定成功")
          break
        end
      end
    end

    for i = 1, 30 do
      pids = EnumProcess(dm, "MapleStory.exe")
      if (pids == "" or pids == nil) then
        show("游戏进程不存在重新开始")
        break
      end
      if (runScript(dm, "进入登录")) then
        show("启动客户端成功开始登录游戏")
        return true
      --show("游戏登录成功")
      --break
      end
      delay(dm, 1000)
    end
    --until true
  end
  -- if (flag == true) then
  --   return true
  -- end
  -- return false
end

--登录
function login(dm, index, threadIndex)
  --SwitchRole(dm,index)
  --loginGame(dm)
  -- nIndexThread = threadIndex
  -- -- show("线程ID"..tostring(nIndexThread))
  -- -- StartTask(dm,index,threadIndex)
  if (longinClient(dm, index, threadIndex)) then --客户端开启成功 --开始登录游戏
    if (loginGame(dm, index, threadIndex)) then
      return StartTask(dm, index, threadIndex)
    end
  end
end

--登录游戏
function loginGame(dm, index)
  --测试开始
  -- local n = GetBindWindow(dm)
  -- if (n > 0) then
  --   show("解除绑定")
  --   UnBindWindow(dm)
  --  --如果存在绑定则解除绑定
  -- end
  -- hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
  -- show("查找窗口句柄" .. tostring(hwnd))
  -- delay(dm,1000)
  -- if(hwnd~=0)
  -- then
  --   ret=BindWindowEx(dm,hwnd)
  --   show("窗口绑定" .. tostring(ret))
  --   if(ret==1)
  --   then
  --     delay(dm,1000)
  --     MoveWindow(dm,hwnd,0,0)
  --     show("窗口绑定成功")

  --   end
  -- end
  -- --测试结束
  -- SetWindowState(dm,hwnd,12)
  while (GetState()) do
    --show("1")
    if (runScript(dm, "账号在线")) then
    --更新列表状态列表状态
    end
    if (runScript(dm, "账号密码错误")) then
    --更新列表状态列表状态
    end
    if (runScript(dm, "二密错误")) then
    --更新列表状态列表状态
    end

    --show("2")
    if (runScript(dm, "进入登录")) then --到达登录页面
      show(GetItemText(index, 1))
      runScripts(dm, "输入账号", GetItemText(index, 1), 1)
      delay(dm, 1000)
      show(GetItemText(index, 2))
      runScripts(dm, "输入密码", GetItemText(index, 2), 1)
      delay(dm, 2000)
      show("点击登录" .. tostring(runScript(dm, "点击登录")))
    end
    --如果为保号
    baohao = GetItemText(index, 11)
    show(baohao)
    if (baohao ~= "" and baohao ~= nil) then
      --选择保号登录
      --选择保号
      show("3")
      delay(dm, 2000)
      runScripts(dm, "选择账号", baohao, 2)
    else
      --选择默认第一账号
      delay(dm, 2000)
      runScript(dm, "选中账号")
    end
    --delay(dm, 2000)
    server = "loginChoseServer" .. GetItemText(index, 4) .. ".bmp"
    --show(server)
    runScripts(dm, "选择区服", server, 2)
    delay(dm, 1000)
    runScript(dm, "选择线路25")
    --取是否输入二密状态
    --delay(dm, 2000)

    if (runScript(dm, "角色页面")) then --到达角色页面且未输入二密
      --选择单一角色方案
      show("进入")
      delay(dm, 1000)
      --二密窗口是否存在不存在则按回车键
      if (runScript(dm, "二密窗口") == false) then
        KeyPress(dm, "enter") --回车弹出二密窗口
      end
      delay(dm, 1000)
      keyWord = GetItemText(index, 3) --二密
      show("二密")
      twoCodeImg = ""
      for i = 1, string.len(keyWord) do
        if (i == string.len(keyWord)) then
          twoCodeImg = twoCodeImg .. string.sub(keyWord, i, i) .. ".bmp"
        else
          twoCodeImg = twoCodeImg .. string.sub(keyWord, i, i) .. ".bmp|"
        end
      end
      show(twoCodeImg)
      strTwoCods = runScriptExs(dm, "二密查找", twoCodeImg)
      tab = split(strTwoCods, "|")
      i = 1
      show("22")
      while (i <= #tab) do
        show("开始")
        --分割字符
        tabs = split(tab[i], ",")
        show(tabs[2] .. "," .. tabs[3])
        ClickEx(dm, tonumber(tabs[2]), tonumber(tabs[3]) + 5, 10, 10)
        delay(dm, math.random(1500, 1800))
        strXing = runScriptExs(dm, "星号查找", "pwdxing.bmp")
        Xings = split(strXing, "|")
        show(strXing)
        if (i ~= #Xings) then
          i = i - 1
        end
        i = i + 1
      end
      if (runScript(dm, "二密确认")) then
        delay(dm, math.random(5500, 6800))
      end
    end

    if (runScript(dm, "登录成功")) then
      show("登录成功")
      return true
    --break;
    end
  end
end

function GetItemText(index, itemIndex) --获取界面列表数据
  if (index == -1) then
    if (itemIndex == 1) then --账号
      return "zgsp46511@daum.net"
    --索引为-1返回默认值
    end
    if (itemIndex == 2) then --密码
      return "zodw147258"
    --索引为-1返回默认值
    end
    if (itemIndex == 3) then --二密
      return "jie2560"
    end
    if (itemIndex == 11) then --保号
      return ""
    --索引为-1返回默认值
    end
    if (itemIndex == 4) then --服务器
      return "2"
    --索引为-1返回默认值
    end
  end
  --show("索引:"..tostring(index))

  return GetItemListText(index, itemIndex)
end

function StartTask(dm, index)
  --测试开始
  -- local n = GetBindWindow(dm)
  -- if (n > 0) then
  --   show("解除绑定")
  --   UnBindWindow(dm)
  -- --如果存在绑定则解除绑定
  -- end
  -- hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
  -- show("查找窗口句柄" .. tostring(hwnd))
  -- delay(dm, 1000)
  -- if (hwnd ~= 0) then
  --   ret = BindWindowEx(dm, hwnd)
  --   show("窗口绑定" .. tostring(ret))
  --   if (ret == 1) then
  --     delay(dm, 1000)
  --     MoveWindow(dm, hwnd, 0, 0)
  --     show("窗口绑定成功")
  --   end
  -- end
  -- --测试结束
  -- SetWindowState(dm, hwnd, 12)

  --delay(dm, 1000)

  -- while (true) do--测试打boss
  --   --AttackWls(dm,index)
  --   KeyDownChar(dm, "right")
  --   delay(dm, 500)
  --   for i=1,30 do
  --     KeyPress(dm,"up")
  --     delay(dm, math.random(50, 80))
  --   end
  --   delay(dm, 3000)
  --   KeyUpChar(dm, "right")

  --   KeyDownChar(dm, "left")
  --   delay(dm, 500)
  --   for i=1,30 do
  --     KeyPress(dm,"up")
  --     delay(dm, math.random(50, 80))
  --   end
  --   delay(dm, 2000)
  --   KeyUpChar(dm, "left")

  -- end

  findbi = false
  --state = GetState()
  while (GetState()) do
    show("线程索引" .. tostring(nIndexThread))
    MoveTo(dm, math.random(1, 1000), math.random(1, 800))
    --获取状态
    --delay(dm, 1000)
    --关闭广告窗口
    RunScriptList(dm, "关闭广告|关闭广告1|登录确定")
    --关闭组队
    --show(runScript(dm, "组队血条"))
    if (runScript(dm, "组队血条")) then
      KeyPress(dm, "p")
      delay(dm, 1000)
      if (runScript(dm, "导航窗口")) then
        runScript(dm, "退出组队")
      end
      delay(dm, math.random(1000, 1500))
      if (runScript(dm, "导航窗口")) then
        KeyPress(dm, "p")
      end
    else
      show("关闭窗口")
      if (runScript(dm, "导航窗口")) then
        KeyPress(dm, "p")
      end
    end
    show("1")

    --交易地图返回
    if (runScript(dm, "交易地图")) then
      KeyPress(dm, "up")
    end
    show("23")
    --冰峰雪域 或 导航
    if (runScript(dm, "冰峰雪域地图") or runScript(dm, "导航按钮")) then
      show("2")
      KeyPress(dm, "t")
      delay(dm, math.random(500, 1000))
      ret, x, y = runScript(dm, "选中乌鲁斯", 0)
      delay(dm, math.random(1000, 1500))
      ClickEx(dm, x + 92, y + 260, 10, 10)
      show(tostring(ret) .. "," .. tostring(x) .. "," .. tostring(y))
    end

    if (runScript(dm, "扎昆祭台入口")) then
      for k = 1, 5 do
        KeyDownChar(dm, "left")
        for i = 1, 10 do
          KeyPress(dm, "up")
          delay(dm, math.random(50, 80))
        end
        KeyUpChar(dm, "left")

        KeyDownChar(dm, "right")
        for i = 1, 11 do
          KeyPress(dm, "up")
          delay(dm, math.random(50, 80))
        end
        KeyUpChar(dm, "right")

        if (runScript(dm, "扎昆祭台入口") == false) then
          break
        end
      end
    end

    --扎昆地图退出
    if (runScript(dm, "扎昆入口")) then
      bMoveto = false
      for i = 1, 5 do
        show("进入扎昆")
        if (runScript(dm, "npc啊杜比斯")) then
          bMoveto = true
          --点击退出
          delay(dm, math.random(500, 1000))
          runScript(dm, "退出扎昆")
          if (runScript(dm, "下一步")) then
            KeyPress(dm, "space")
          end
        else
          if (bMoveto == false) then
            KeyDownChar(dm, "right")
            delay(dm, math.random(2000, 3000))
            KeyUpChar(dm, "right")
          end
        end
      end
    end
    RunScriptList(dm, "关闭广告|关闭广告1|登录确定")
    if (findbi == false) then
      --打开显示电脑百分比
      repeat
        --如果为扎昆地图退出查找
        if (runScript(dm, "扎昆入口") or runScript(dm, "扎昆祭台入口")) then
          break
        end
        if (runScript(dm, "红电脑")) then
          show("列表设置红电脑")
          findbi = true
          break
        end
        if (runScript(dm, "设置") == false) then
          KeyPress(dm, "esc")
        end
        delay(dm, math.random(1000, 1500))
        show(tostring(runScript(dm, "设置")))
        if (runScript(dm, "设置")) then
          --设置为假则按esc
          --KeyPress(dm, "esc")
          delay(dm, math.random(500, 1000))
          ret, x, y = runScript(dm, "设置", 0)
          show("设置坐标" .. tostring(x) .. "," .. tostring(y))
          ClickEx(dm, x + 15, y + 90, 10, 3)
          delay(dm, math.random(500, 1000))
          if (runScript(dm, "电脑列表")) then --打开列表窗口
            delay(dm, math.random(500, 1000))
            if (runScript(dm, "电脑百分比")) then
              show("显示电脑列表")
            end
            delay(dm, math.random(500, 1000))
          end
          KeyPress(dm, "esc")
          findbi = true
        end
      until true
    end
    RunScriptList(dm, "关闭广告|关闭广告1|登录确定")
    if (GetState() and findbi == true) then
      show("查找npc")
      --查找乌鲁斯npc
      if (runScript(dm, "乌鲁斯") or runScript(dm, "乌鲁斯1") or runScript(dm, "乌鲁斯2")) then
        delay(dm, math.random(500, 1000))
        for i = 1, 5 do
          show("查找选项")
          runScript(dm, "乌鲁斯选择")
          delay(dm, math.random(300, 500))
          if (runScript(dm, "乌鲁斯下一步")) then
            KeyPress(dm, "space")
          end
          --delay(dm, math.random(200, 300))
        end
      else
        KeyDownChar(dm, "right")
        delay(dm, math.random(3000, 3500))
        KeyUpChar(dm, "right")
      end
      --未找到npc移动

      show("22")
      --开始乌鲁斯任务
      if (runScript(dm, "等待组队")) then
        MoveTo(dm, math.random(1, 1000), math.random(1, 800))
        while (true) do --boss环节
          show("进入等待环节")
          runScript(dm, "等待组队")
          if (runScript(dm, "组队确定")) then
            delay(dm, math.random(200, 300))
            MoveTo(dm, math.random(1, 1000), math.random(1, 800))
          end
          SetBossState(true)
          if (runScript(dm, "进入乌鲁斯")) then
            --开始记录打击次数
            --记录结束
            --设置线程查找boss
            show("开始攻击")
            --GetBossState()
            if (AttackWls(dm, index) == true) then
              --退出游戏
              delay(dm, math.random(8000, 8500))
              -- for j = 1, 100 do
              --   --向右移动9秒
              --   if (runScript(dm, "乌鲁斯退出1")) then
              --     show("乌鲁斯退出1")
              --     KeyDownChar(dm, "right")
              --     delay(dm, 2000)
              --     KeyDownChar(dm,"up")
              --     for i = 1, 30 do
              --       if (runScript(dm, "乌鲁斯地图")) then
              --         KeyUpChar(dm,"up")
              --         break
              --       end
              --     end
              --     delay(dm, 1000)
              --     KeyUpChar(dm, "right")
              --     KeyUpChar(dm,"up")
              --     KeyDownChar(dm, "left")
              --     delay(dm, 500)
              --     KeyDownChar(dm,"up")
              --     for i = 1, 30 do
              --       if (runScript(dm, "乌鲁斯地图")) then
              --         KeyUpChar(dm,"up")
              --         break
              --       end
              --     end
              --   end
              --   delay(dm, 2000)
              --   KeyUpChar(dm, "left")
              --   if (runScript(dm, "乌鲁斯地图")) then
              --     break
              --   end
              -- end
              ExitWls(dm,index)
              break
            --退出boss
            end
          end


          if (runScript(dm, "乌鲁斯结束")) then
            ExitWls(dm,index)
            break
          end


          --进入乌鲁斯结束
        end
        show("结束攻击")
      --boss循环结束
      end

    --等待结束
    end
    --检查boss是否已完成
    --show("检查结束")
    if (runScript(dm, "乌鲁斯结束1")) then
      findbi=false
      show("乌鲁斯结束")
      KeyPress(dm, "esc")
      --设定列表状态

      show("当前完成索引"..tostring(index))
      
      SetItemListText(index, 6, "完成")
      index = index + 1
      SetConfig(tonumber(index), "state", "1", 0)
      
      show("索引下标。" .. tostring(index))
      line = GetConfig("Set", "CodeLine", 1)
      line1 = GetItemListText(index, 12)
      show("line..." .. tostring(line))
      show("line1..." .. tostring(line1))
      if (line == line1) then
        show("进入切换角色")
        SwitchRole(dm, index)
      else
        return true
      end
    --切换账号
    end
  end
  --delay(dm, math.random(200, 300))
end



function ExitWls(dm,index)
  for j = 1, 100 do
    --向右移动9秒
    if (runScript(dm, "乌鲁斯退出1")) then
      show("乌鲁斯退出1")
      KeyDownChar(dm, "right")
      delay(dm, 2000)
      KeyDownChar(dm,"up")
      for i = 1, 50 do
        delay(dm, math.random(30, 50))
      end
      delay(dm, 1000)
      KeyUpChar(dm, "right")
      KeyUpChar(dm,"up")

      if (runScript(dm, "乌鲁斯地图")) then
        KeyUpChar(dm,"up")
        return true
      end
      KeyDownChar(dm, "left")
      delay(dm, 500)
      KeyDownChar(dm,"up")
      for i = 1, 50 do
        delay(dm, math.random(30, 50))
      end
      if (runScript(dm, "乌鲁斯地图")) then
        KeyUpChar(dm,"up")
        return true
      end
    end
    delay(dm, 2000)
    KeyUpChar(dm, "left")
    if (runScript(dm, "乌鲁斯地图")) then
      return true
    end
  end
end





function SwitchRole(dm, index)
  -- index=0
  -- show("1")
  --   local n = GetBindWindow(dm)
  -- if (n > 0) then
  --   show("解除绑定")
  --   UnBindWindow(dm)
  -- --如果存在绑定则解除绑定
  -- end
  -- hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
  -- show("查找窗口句柄" .. tostring(hwnd))
  -- delay(dm, 1000)
  -- if (hwnd ~= 0) then
  --   ret = BindWindowEx(dm, hwnd)
  --   show("窗口绑定" .. tostring(ret))
  --   if (ret == 1) then
  --     delay(dm, 1000)
  --     MoveWindow(dm, hwnd, 0, 0)
  --     show("窗口绑定成功")
  --   end
  -- end
  -- --测试结束
  -- SetWindowState(dm, hwnd, 12)
  show("开始切换角色")
  while (GetState()) do
    show("切换角色")
    delay(dm, 1000)
    ret, x, y = runScript(dm, "设置按钮", 0)
    if (ret == true) then
      ClickEx(dm, x, y, 5, 5)
      delay(dm, 3000)
      ClickEx(dm, x - 20, y - 35, 20, 5)
      delay(dm, 1000)
    end
    if (runScript(dm, "点击退出")) then
      delay(dm, 5000)
    end
    runScript(dm, "退出角色")
    runScript(dm, "确定退出角色")
    if (runScript(dm, "进入登录")) then
      show("启动客户端成功开始登录游戏")
      break
    end
    --runScript(dm, "点击退出")
    --runScript(dm, "退出角色")
  end
  show("切换角色索引："..tostring(index))
  loginGame(dm, index)
end

-- function GetBossPoint()
--   return math.random(1, 1024)
-- end

function AttackWls(dm, index)
  k = 1
  key = ""
  j = 1
  while (k < 18) do
    repeat
      if (runScript(dm, "乌鲁斯结束")) then
        return true
      end
      x = GetBossPoint() --获取boss坐标
      show("boss坐标：" .. tostring(x))
      if (x > 521 and x < 850) then
        --Attack(dm,index,"right")
        KeyPress(dm, "right")
        for i = 1, 20 do
          KeyDownChar(dm, "shift")
          delay(dm, math.random(50, 100))
          KeyUpChar(dm, "shift")
          if (i % 5 == 0) then
            x = GetBossPoint()
            if (x < 520) then
              k = 1
              key = "right"
              break
            end
          end
        end
        k = 1
      elseif (x > 850) then
        KeyUpChar(dm, "left")
        KeyDownChar(dm, "right")
        delay(dm, math.random(1800, 2000))
        KeyUpChar(dm, "right")
        for i = 1, 20 do
          KeyDownChar(dm, "shift")
          delay(dm, math.random(50, 100))
          KeyUpChar(dm, "shift")

          if (i % 10 == 0) then
            x = GetBossPoint()
            if (x < 520) then
              k = 1
              key = "right"
              break
            end
          end
        end

        k = 1
      elseif (x > 150 and x < 500) then
        KeyPress(dm, "left")
        for i = 1, 20 do
          KeyDownChar(dm, "shift")
          delay(dm, math.random(50, 100))
          KeyUpChar(dm, "shift")
          if (i % 10 == 0) then
            x = GetBossPoint()
            if (x < 520) then
              k = 1
              key = "left"
              break
            end
          end
        end

        --Attack(dm,index,"left")
        k = 1
      elseif (x < 150 and x > 1) then
        KeyUpChar(dm, "right")
        KeyDownChar(dm, "left")
        delay(dm, math.random(1500, 1800))
        KeyUpChar(dm, "left")
        for i = 1, 20 do
          KeyDownChar(dm, "shift")
          delay(dm, math.random(50, 100))
          KeyUpChar(dm, "shift")
          if (i % 10 == 0) then
            x = GetBossPoint()
            if (x < 520) then
              k = 1
              key = "left"
              break
            end
          end
        end
        --MoveAttack(dm,index,"left")
        k = 1
      end

      k = k + 1
      JumpAttack(dm, index, key)

      if (k > 4 and k < 10) then
        show("5")
        KeyUpChar(dm, "right")
        JumpAttack(dm, index, "left")
      end
      if (k > 11 and k < 17) then
        KeyUpChar(dm, "left")
        JumpAttack(dm, index, "right")
      end
      delay(dm, math.random(200, 300))
    until true
  end
  return false
end

function Attack(dm, index) --直接攻击
  for i = 1, 6 do
    KeyDownChar(dm, "shift")
    delay(dm, math.random(150, 200))
    KeyUpChar(dm, "shift")
  end
end

function MoveAttack(dm, index, key) --移动后直接攻击
  KeyDownChar(dm, key)
  delay(dm, math.random(1800, 2000))
  KeyUpChar(dm, key)
  Attack(dm, index)
end

function JumpAttack(dm, index, key) --移动后直接攻击
  -- KeyDownChar(dm,key)
  -- delay(dm, math.random(1800, 2000))
  -- KeyUpChar(dm,key)
  -- Attack(dm,index)

  KeyDownChar(dm, key)

  delay(dm, 100)
  KeyPress(dm, "alt")

  delay(dm, 100)
  KeyPress(dm, "alt")

  KeyUpChar(dm, key)
  delay(dm, 500)
end

-- function MoveAttack(dm,key)

-- end

function GetState1()
  return 0
end

function sleep(dm, nDelay)
  delay(dm, nDelay)
end

function RunScriptList(dm, str)
  list = split(str, "|")
  for key, value in ipairs(list) do
    runScript(dm, value)
  end
end

function login1(dm)
  if (runScript(dm, "进入登录")) then
    show("游戏登录成功")
  end
end

function split(str, reps)
  local resultStrList = {}
  string.gsub(
    str,
    "[^" .. reps .. "]+",
    function(w)
      table.insert(resultStrList, w)
    end
  )
  return resultStrList
end

function ZhaKunBoss(dm, index)
  local n = GetBindWindow(dm)
  if (n > 0) then
    show("解除绑定")
    UnBindWindow(dm)
  --如果存在绑定则解除绑定
  end
  hwnd = FindWindowEx(dm, "MapleStoryClass", "MapleStory")
  show("查找窗口句柄" .. tostring(hwnd))
  delay(dm, 1000)
  if (hwnd ~= 0) then
    ret = BindWindowEx(dm, hwnd)
    show("窗口绑定" .. tostring(ret))
    if (ret == 1) then
      delay(dm, 1000)
      MoveWindow(dm, hwnd, 0, 0)
      show("窗口绑定成功")
    end
  end
  --测试结束
  SetWindowState(dm, hwnd, 12)

  local bSwichLine = false
  gstate = 0
  for i = 1, 5 do
    --CloseTeam(dm,index)
    ClickChoseZhaKun(dm, index)
     --选择扎昆
    if (bSwichLine == false and runScript(dm, "扎昆入口")) then
      show("换线开始" .. tostring(bSwichLine))
      bSwichLine = SwitchLine(dm, index)
      show("换线结束" .. tostring(bSwichLine))
    else
      MoveToNpc(dm, index)
     --移动到npc
    end

    if (runScript(dm, "扎昆祭台入口")) then
      if (runScript(dm, "npc啊杜比斯")) then
        for k = 1, 5 do
          if (runScript(dm, "下一步")) then
            KeyPress(dm, "space")
            delay(dm, math.random(500, 800))
            if (runScript(dm, "扎昆已刷")) then
            --变更状态切换角色
            end
          end
        end
      end
    end
    if (runScript(dm, "扎昆祭台地图")) then
      if (gstate == 0) then
        delay(dm, math.random(200, 300))
        KeyPress(dm, "1")
        delay(dm, math.random(4000, 4500))
        gstate = 1
      end
      if (gstate == 1) then
        KeyDownChar(dm, "right")
        delay(dm, math.random(3000, 3200))
        KeyUpChar(dm, "right")
        gstate = 2
      end
      if (gstate == 2) then
        if (runScript(dm, "npc阿们")) then
          KeyDownChar(dm, "left")
          delay(dm, math.random(1500, 1600))
          KeyUpChar(dm, "left")
          gstate = 3
        end
      end
      if (gstate == 3) then
      end
    end
  end
end

function ZhaSwichRole(dm, index)
  --设定状态
  --tuic
end

function CloseTeam(dm, index) --关闭组队
  show("关闭组队")
  if (runScript(dm, "组队血条")) then
    KeyPress(dm, "p")
    delay(dm, 1000)
    if (runScript(dm, "导航窗口")) then
      runScript(dm, "退出组队")
    end
    delay(dm, math.random(1000, 1500))
    if (runScript(dm, "导航窗口")) then
      KeyPress(dm, "p")
    end
  else
    show("关闭窗口")
    if (runScript(dm, "导航窗口")) then
      KeyPress(dm, "p")
    end
  end
end

--选择扎昆boss
function ClickChoseZhaKun(dm, index)
  --是否在村庄或
  runScript(dm, "确定按钮")
  if (runScript(dm, "冰峰雪域地图") or runScript(dm, "导航按钮")) then
    show("2")
    KeyPress(dm, "t")
    delay(dm, math.random(1000, 2000))
    ret, x, y = runScript(dm, "导航窗口", 0)
    --s(x)
    if (ret == true) then
      ClickEx(dm, x, y + math.random(70, 77), 5, 5)
      delay(dm, math.random(500, 800))
      ClickEx(dm, x + 125, y + 338, 10, 10)
      for i = 1, 5 do
        runScript(dm, "确定按钮")
        delay(dm, math.random(200, 300))
        --已经刷过设定状态  未完成
      end
    end
  end
end

--移动到npc
function MoveToNpc(dm, index)
  --查找坐标
  ret1, x1, y1 = runScript(dm, "小地图", 0)

  if (runScript(dm, "扎昆入口")) then
    show("移动到扎昆入口")
    ret, x, y = runScript(dm, "人物坐标", 0)
    s(x)
    k = x - x1
    s(k)
    if (x - x1 < 115) then
      -- show("退出移动")
      -- return
      KeyDownChar(dm, "right")
      for i = 1, 20 do
        ret, x, y = runScript(dm, "人物坐标", 0)
        k = x - x1
        s(k)
        if (x - x1 > 118) then
          KeyUpChar(dm, "right")
          break
        end
        delay(dm, math.random(500, 1000))
      end
    else
      KeyDownChar(dm, "right")
      for k = 1, 20 do
        KeyPress(dm, "up")
        delay(dm, math.random(30, 50))
        if (runScript(dm, "扎昆进场")) then
          return
        end
      end
      KeyUpChar(dm, "right")
    end
  end
end

--换线
function SwitchLine(dm, index)
  if (runScript(dm, "扎昆入口")) then
    if (runScript(dm, "设置") == false) then
      KeyPress(dm, "esc")
    end
    delay(dm, math.random(500, 1000))
    KeyPress(dm, "enter")
    delay(dm, math.random(500, 1000))
    if (runScript(dm, "选择9线")) then
      return true
    end
    if (runScript(dm, "已选9线")) then
      delay(dm, math.random(500, 1000))
      KeyPress(dm, "esc")
      return true
    end
  else
    return false
  end
end

function s(intx, inty)
  show("坐标" .. tostring(intx))
end
